import java.util.Arrays;

public class Solution31 {
    public void nextPermutation(int[] nums) {
        int t=nums.length-1;
        while (t>0 && nums[t]<=nums[t-1]){
            t--;
        }
        int start=0;
        if (t!=0){
            int p=t;
            while (p<nums.length && nums[p]>nums[t-1]){
                p++;
            }
            int temp=nums[t-1];
            nums[t-1]=nums[p-1];
            nums[p-1]=temp;
            start=t;
        }
        for (int l=start,r=nums.length-1;l<r;l++,r--){
            int temp=nums[l];
            nums[l]=nums[r];
            nums[r]=temp;
        }
    }

    public static void main(String[] args) {
        int[] nums=new int[]{1,2,3};
        new Solution31().nextPermutation(nums);
        System.out.println(Arrays.toString(nums));
    }
}
